//
//  FishScene.m
//  fish
//
//  Created by 袁飞 on 12-8-28.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "FishScene.h"
#import "FishManager.h"
#import "Player.h"
#import "FishAI.h"
#import "FWnd.h"

@implementation FishScene
static FishScene *_sharedFishScene = nil;
+ (FishScene *)sharedFishScene
{
  if (!_sharedFishScene) {
    _sharedFishScene = [[FishScene alloc] init];
    srand((unsigned int)time(NULL));
  }
  return _sharedFishScene;
}

-(id)init{
  [super init];
  [self setIsTouchEnabled:true];
  fish_layer_ = [[CCLayer alloc] init];
  bk_layer_ = [[CCLayer alloc] init];
  [[FishManager sharedFishManager] genFishes];
  /*NSMutableArray *fishes = [[FishManager sharedFishManager] fishes];    
  for(FishData* fd in fishes){
    CGPoint p = [fd position];
    p = ccp(p.x, -p.y+160);
    [fd setPosition:p];
    [fish_layer_ addChild:fd];
    FAI *ai = [[FishAI alloc] initWithFObject:fd];
    [fd setAi:ai];
    [fd startAi];
  }*/
  FWnd *wnd = [[FWnd alloc] initFromFile:@"wnd.xml"];
  [self addChild:wnd];
  [wnd setPosition:ccp(240, 160)];
  
  FWnd *headwnd = [[FWnd alloc] initFromFile:@"wnd.xml"];
  [self addChild:wnd];
  [wnd setPosition:ccp(240, 160)];
  

  [self addChild:fish_layer_];
  [self addChild:[Player sharedPlayer]];
  return self;
}

-(void)registerWithTouchDispatcher{
  [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:kCCMenuTouchPriority swallowsTouches:false];
}

-(void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event{
  CGPoint point = [touch locationInView:[touch view]];
  point = [[CCDirector sharedDirector] convertToGL:point];
  
  [self unschedule:@selector(onTimer)];
  direction_ = ccp(0, 0);
  
  Player *pl = [Player sharedPlayer];
  //[pl runAction:@"blow" WithSprite:@"bullet"];
  [pl fireWithAngle:point];
}

-(BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event{
  CGPoint point = [touch locationInView:[touch view]];
  point = [[CCDirector sharedDirector] convertToGL:point];
  NSLog(@"bbb %d,%d", point.x, point.y);
  
  CGRect left = CGRectMake(0, 140, 40, 40);
  CGRect right = CGRectMake(440, 140, 40, 40);
  CGRect up = CGRectMake(200, 280, 40, 40);
  CGRect down = CGRectMake(200, 0, 40, 40);

  if (CGRectContainsPoint(left, point)) {
    direction_ = ccp(-1, 0);    
  }else if (CGRectContainsPoint(right, point)){
    direction_ = ccp(1, 0);    
  }else if (CGRectContainsPoint(up, point)){
    direction_ = ccp(0, -1);    
  }else if (CGRectContainsPoint(down, point)){
    direction_ = ccp(0, 1);    
  }
  if (direction_.x!=0 || direction_.y!=0) {
    [self schedule:@selector(onTimer) interval:0.02f];
  }
  return true;
}

-(void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event{

}

-(void)showPlayerWnd{
  FWnd *wnd = [[FWnd alloc] initFromFile:@"wnd.xml"];
  [wnd setPosition:ccp(240, 160)];
  [self addChild:wnd];
}

-(void)onTimer{
  if(direction_.x != 0){
    Player *pl = [Player sharedPlayer];
    CGPoint p = [pl position];
    [pl setPosition:ccp(p.x+direction_.x*5, p.y)];
  }
  
  if(direction_.y != 0){
    CGPoint p = [fish_layer_ position];
    [fish_layer_ setPosition:ccp(p.x, p.y+direction_.y*5)];
  }
}
@end
